package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.com.ruanmeng.demon.GetYZMM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.DesUtils;
import com.com.ruanmeng.utils.TimeCount;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 获取验证码
 * 注意  获取之后变成重新获取
 */
public class GetYanZhengMaActivity extends BaseActivity {

    @BindView(R.id.imv_lay_back)
    ImageView imvLayBack;
    @BindView(R.id.tv_lay_title)
    TextView tvLayTitle;
    @BindView(R.id.tv_lay_next)
    TextView tvLayNext;
    @BindView(R.id.ed_register_yzm)
    EditText edRegisterYzm;
    @BindView(R.id.btn_getma)
    Button btnGetma;
    @BindView(R.id.bt_getma_next)
    Button btGetmaNext;
    @BindView(R.id.iv_getma_del)
    ImageView ivGetmaDel;
    private TimeCount time;
    private String yanzhengma;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_yan_zheng_ma);
        ButterKnife.bind(this);
        AddLoginActivity(this);
        if (TextUtils.isEmpty(getIntent().getStringExtra("forget"))) {
            ChangLayTitle("注册");
        } else {
            ChangLayTitle("重置密码");
        }
        LayBack();
        init();
        time = new TimeCount(GetYanZhengMaActivity.this, 60000, 1000, btnGetma);// 构造CountDownTimer对象
    }

    public void init() {
        edRegisterYzm.addTextChangedListener(this);
        btGetmaNext.setClickable(false);
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        super.onTextChanged(charSequence, i, i1, i2);
        if (TextUtils.isEmpty(edRegisterYzm.getText().toString())) {
            btGetmaNext.setBackgroundResource(R.drawable.ed_fullgeray);
            btGetmaNext.setClickable(false);
            ivGetmaDel.setVisibility(View.GONE);
        } else {
            btGetmaNext.setBackgroundResource(R.drawable.ed_fullmain);
            btGetmaNext.setClickable(true);
            ivGetmaDel.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.btn_getma, R.id.bt_getma_next, R.id.iv_getma_del})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_getma://获取验证码
                if (TextUtils.isEmpty(getIntent().getStringExtra("forget"))) {
                    getRegisterYZMData();
                } else {
                    getFindPswYZMData();
                }
                break;
            case R.id.bt_getma_next://下一步
                if (TextUtils.isEmpty(edRegisterYzm.getText().toString())) {
                    CommonUtil.showToask(GetYanZhengMaActivity.this, "请输入验证码！");
                    return;
                }
                //对比验证码是否正确
                if (!edRegisterYzm.getText().toString().equals(yanzhengma)) {
                    CommonUtil.showToask(GetYanZhengMaActivity.this, "请输入正确的验证码！");
                    return;
                }
                Intent in = new Intent(GetYanZhengMaActivity.this, RegisterSetPswActivity.class);
                in.putExtra("forget", getIntent().getStringExtra("forget"));
                in.putExtra("phone", getIntent().getStringExtra("phone"));
                in.putExtra("yzm", yanzhengma);
                startActivity(in);
                break;
            case R.id.iv_getma_del:
                edRegisterYzm.setText("");
                break;
        }
    }

    /**
     * 获取验证码
     */
    public void getRegisterYZMData() {
        Date dt= new Date();
        Long timelong= dt.getTime();
        DesUtils.DESIV = DesUtils.getiv(timelong+"");
        String key1 = DesUtils.getkey(timelong+"");
        String content = DesUtils.encode(key1, getIntent().getStringExtra("phone"));
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.GetMa, Const.POST);
        mRequest.add("mobile", content);
        mRequest.add("time",timelong);
        // 添加到请求队列
        CallServer.getRequestInstance().add(GetYanZhengMaActivity.this, 0, mRequest,
                new CustomHttpListener(GetYanZhengMaActivity.this, true, GetYZMM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        GetYZMM model = ((GetYZMM) data);
                        time.start();
                        yanzhengma = model.getObject();
//                        CommonUtil.showToask(GetYanZhengMaActivity.this,""+yanzhengma);
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(GetYanZhengMaActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }

    /**
     * 获取找回密码验证码
     */
    public void getFindPswYZMData() {
        Date dt= new Date();
        Long timelong= dt.getTime();
        DesUtils.DESIV = DesUtils.getiv(timelong+"");
        String key1 = DesUtils.getkey(timelong+"");
        String content = DesUtils.encode(key1, getIntent().getStringExtra("phone"));
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.FindPswGetMa, Const.POST);
        mRequest.add("mobile", content);
        mRequest.add("time",timelong);
        // 添加到请求队列
        CallServer.getRequestInstance().add(GetYanZhengMaActivity.this, 0, mRequest,
                new CustomHttpListener(GetYanZhengMaActivity.this, true, GetYZMM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        GetYZMM model = ((GetYZMM) data);
                        time.start();
                        yanzhengma = model.getObject();
//                        CommonUtil.showToask(GetYanZhengMaActivity.this,""+yanzhengma);
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(GetYanZhengMaActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }


}
